package com.wyd.filter;

import com.wyd.entity.Context;
import com.wyd.entity.DseProductPriorityInfo;
import lombok.extern.slf4j.Slf4j;
import org.jeasy.rules.annotation.Action;
import org.jeasy.rules.annotation.Condition;
import org.jeasy.rules.annotation.Fact;
import org.jeasy.rules.annotation.Rule;

import java.util.Iterator;

@Rule(name = "terminal_forbid_filter", description = "终端禁止过滤", priority = 2)
@Slf4j
public class TerminalForbidFilter {

  @Condition
  public boolean when() {
    return true;
  }

  @Action
  public void action(@Fact("ct") Context ct) {
      log.info("开始执行>>>终端禁止过滤");

      if(ct.getDseProductList() == null || ct.getDseProductList().isEmpty()){
          return;
      }

      //存在权益禁止产品列表且产品存在于禁止列表里面则过滤
      if(ct.getTerminalForbidDseProductIdList() != null && ct.getTerminalForbidDseProductIdList().size() > 0) {
          Iterator<DseProductPriorityInfo> iterator = ct.getDseProductList().iterator();
          while (iterator.hasNext()) {
              DseProductPriorityInfo info = iterator.next();
              if (ct.getTerminalForbidDseProductIdList().contains(info.getDseProductId())) {
                  iterator.remove();
                  log.info("终端禁止产品过滤产品id>>>{}", info.getDseProductId());
              }
          }
      }
  }
}
